package com.campus.entity;

import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.campus.entity.vo.ScheduleVO;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;

/**
 * @TableName schedule
 */
@TableName(value = "schedule")
@Data
public class Schedule implements Serializable {


    private static final long serialVersionUID = 1L;
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private String account;
    private String title;
    private String tips;
    private Timestamp beginTime;
    private Timestamp endTime;
    /**
     * 删除标志（0代表未删除，1代表已删除）
     */
    @TableLogic
    @TableField(value = "del_flag")
    private Integer delFlag;
    /**
     * 完成标志（0代表未完成，1代表已完成）
     */
    @TableField(value = "is_over")
    private Integer isOver;

    public Schedule(ScheduleVO vo) {
        this.id = Integer.valueOf(vo.getId());
        this.account = vo.getAccount();
        this.title = vo.getTitle();
        this.tips = vo.getTips();
        this.isOver = vo.getIsOver();
        this.delFlag = vo.getDelFlag();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        try {
            this.beginTime = new Timestamp(sdf.parse(format(vo.getBeginTime())).getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.endTime = new Timestamp(beginTime.getTime() + (long) Integer.parseInt(vo.getDuration()) * 1000 * 60);
    }

    public Schedule() {
    }

    private String format(String time) {
        String res = "";
        int b = 0;
        int a = 0;
        a = time.indexOf('y') + 1;
        res += time.substring(a, a + 4);
        a = time.indexOf('m') + 1;
        b = time.indexOf('d');
        if (b - a == 1) {
            res += "0";
        }
        res += time.substring(a, b);
        a = b + 1;
        b = time.indexOf(' ');
        if (b - a == 1) {
            res += "0";
        }
        res += time.substring(a, b);
        a = b + 1;
        b = time.indexOf(':');
        if (b - a == 1) {
            res += "0";
        }
        res += time.substring(a, b);
        res += time.substring(b + 1);
        System.err.println(res);
        return res;
    }

}